package com.generic.alerts
{
    import com.generic.alerts.simple.ReportBase;
    import com.generic.alerts.simple.SimpleBase;
    import com.generic.alerts.simple.TodoBase;
    
    import flash.display.DisplayObject;
    
    import mx.core.Application;
    import mx.core.IFlexDisplayObject;
    import mx.events.FlexEvent;
    import mx.managers.PopUpManager;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;

    public class GenericAlert
    {

        [Embed(source="../assets/action_success-48.png")]
        private static var successIcon : Class; 
        [Embed(source="../assets/action_fail-48.png")] 
        private static var failureIcon : Class;
        [Embed(source="../assets/messagebox_warning-48.png")]        
        private static var warningIcon : Class;      
        [Embed(source="../assets/messagebox_critical-48.png")]        
        private static var errorIcon : Class;
        [Embed(source="../assets/documentinfo-48.png")]        
        private static var infoIcon : Class;
        [Embed(source="../assets/irkickflash-48.png")]        
        private static var webServiceIcon : Class;
        [Embed(source="../assets/agt_resume-48.png")]        
        private static var debugIcon : Class;
        [Embed(source="../assets/question-48.png")]        
        private static var questionIcon : Class;
        [Embed(source="../assets/kgpg_edit.png")]        
        private static var validationIcon : Class;

        public static function success(message : String, title : String = null, confirmFunction : Function = null, cancelFunction : Function = null) : void{
            showSimple(message, title ? title : "Success", successIcon, "OK", null, confirmFunction, cancelFunction);
        }

        public static function failure(message : String, title : String = null, confirmFunction : Function = null, cancelFunction : Function = null) : void{
            showSimple(message, title ? title : "Failure", failureIcon, "OK", null, confirmFunction, cancelFunction);
        }

        public static function notification(message : String, title : String = null, confirmFunction : Function = null, cancelFunction : Function = null) : void{
            showSimple(message, title ? title : "Notification", infoIcon, "OK", null, confirmFunction, cancelFunction);
        }

        public static function confirmation(message : String, title : String = null, confirmFunction : Function = null, cancelFunction : Function = null) : void{
            showSimple(message, title ? title : "Confirmation", questionIcon, "OK", "Cancel", confirmFunction, cancelFunction);
        }

        public static function question(message : String, title : String = null, confirmFunction : Function = null, cancelFunction : Function = null) : void{
            showSimple(message, title ? title : "Question", questionIcon, "Yes", "No", confirmFunction, cancelFunction);
        }

        public static function validation(message : String, title : String = null, confirmFunction : Function = null, cancelFunction : Function = null) : void{
            showSimple(message, title ? title : "Validation", validationIcon, "OK", null, confirmFunction, cancelFunction);
        }

        public static function warning(message : String, title : String = null, confirmFunction : Function = null, cancelFunction : Function = null) : void{
            showSimple(message, title ? title : "Warning", warningIcon, "OK", null, confirmFunction, cancelFunction);
        }

        public static function error(message : String, title : String = null, confirmFunction : Function = null, cancelFunction : Function = null) : void{
            showSimple(message, title ? title : "Error", errorIcon, "OK", null, confirmFunction, cancelFunction);
        }
		
        public static function serverError(event : ResultEvent, message : String, title : String = null, confirmFunction : Function = null, cancelFunction : Function = null) : void{
            showSimple(message, title ? title : "Server Error", errorIcon, "OK", null, confirmFunction, cancelFunction);

        }

        public static function webServiceError(event : FaultEvent, message : String = null, title : String = null, confirmFunction : Function = null, cancelFunction : Function = null) : void{
            showSimple(event.message + "\n\n"+message, title ? title : "Web Service Error", webServiceIcon, "OK", null, confirmFunction, cancelFunction);

        }

        public static function debug(message : String, title : String = null, confirmFunction : Function = null, cancelFunction : Function = null) : void{
            showSimple(message, title ? title : "Debug", debugIcon, "OK", null, confirmFunction, cancelFunction);

        } 

        public static function todo(message : String = "Not yet implemented", responsible : String = "") : void{

            var todoBase : TodoBase = new TodoBase();
            todoBase.message = message;
            todoBase.responsible = responsible;

            todoBase.addEventListener(GenericAlertEvent.CANCEL, function (event : GenericAlertEvent) : void{
                    PopUpManager.removePopUp(event.currentTarget as IFlexDisplayObject);                    
                });
            todoBase.addEventListener(GenericAlertEvent.CONFIRM, function (event : GenericAlertEvent) : void{
                    PopUpManager.removePopUp(event.currentTarget as IFlexDisplayObject);                   
                });

            PopUpManager.addPopUp(todoBase, Application.application as DisplayObject, true);
            todoBase.addEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete);

        }

        public static function report(reportMessage : String, location : String, customMessage : String = "") : void{
            var report : ReportBase = new ReportBase();

            report.text = reportMessage;
            report.location = location;
            report.customMessage = customMessage;

            report.addEventListener(GenericAlertEvent.CANCEL, function (event : GenericAlertEvent) : void{
                    PopUpManager.removePopUp(event.currentTarget as IFlexDisplayObject);                    
                });
            report.addEventListener(GenericAlertEvent.CONFIRM, function (event : GenericAlertEvent) : void{
                    PopUpManager.removePopUp(event.currentTarget as IFlexDisplayObject);                   
                });

            PopUpManager.addPopUp(report, Application.application as DisplayObject, true);
            report.addEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete);
        }

        /////////////////////////////////////////////////////////
        ////// Private methods //////////////////////////////////
        /////////////////////////////////////////////////////////

        private static function showSimple(message : String, title : String, icon : Class, leftButtonLabel : String = null, rightButtonLabel : String = null, confirmFunction : Function = null, cancelFunction : Function = null) : void{

            var simple : SimpleBase = new SimpleBase();

            simple.message = message;
            simple.title = title;
            simple.iconClass = icon;
            simple.firstLabel = leftButtonLabel;
            simple.secondLabel = rightButtonLabel;
            simple.addEventListener(GenericAlertEvent.CANCEL, function (event : GenericAlertEvent) : void{
                    PopUpManager.removePopUp(event.currentTarget as IFlexDisplayObject);
                    if(cancelFunction != null){
                        cancelFunction();
                    }
                });
            simple.addEventListener(GenericAlertEvent.CONFIRM, function (event : GenericAlertEvent) : void{
                    PopUpManager.removePopUp(event.currentTarget as IFlexDisplayObject);
                    if(confirmFunction != null){
                        confirmFunction();
                    }
                });

            PopUpManager.addPopUp(simple, Application.application as DisplayObject, true);
            simple.addEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete);
        }

        private static function onCreationComplete(event : FlexEvent) : void{
            event.target.removeEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete);
            PopUpManager.centerPopUp(event.target as IFlexDisplayObject);
            event.target.setFocus();
        }



    }
}

